<?php
// +----------------------------------------------------------------------
// | Yzncms [ 御宅男工作室 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2007 http://yzncms.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: fastadmin: https://www.fastadmin.net/
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\admin\model\CarModel;
use app\admin\model\College;
use app\admin\model\UserModel;
use app\common\controller\Adminbase;
use app\common\model\AliPay;
use app\common\model\BannerModel;
use app\common\model\MemberModel;
use app\common\model\RebateLog;
use util\Tree;


class Card extends Adminbase
{

    protected function initialize()
    {
        parent::initialize();
        $this->modelClass = new \app\common\model\CardModel();
    }

    public function index()
    {
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($page, $limit, $where, $sort, $order) = $this->buildTableParames();
            $list = $this->modelClass
                ->with('user')
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);

            foreach ($list as $key => &$value) {
                $value['card1'] = config('app.api_url') . $value['card1'];
                $value['card2'] = config('app.api_url') . $value['card2'];
            }
            $result = ["code" => 0, 'count' => $list->total(), "data" => $list->items()];
            return json($result);
        }
        return $this->fetch();
    }


    public function edit()
    {
        $id = $this->request->param('id/d', 0);
        $row = $this->modelClass->get($id);
        if ($this->request->isPost()) {
            $params = $this->request->post('row');
            try {
                $row->allowField(true)->save($params);
            } catch (\Exception $e) {
                $this->error($e->getMessage());
            }
            $this->success("修改成功！");
        }
        $userInfo = (new \app\common\model\UserModel())->where(['id' => $row['uid']])->find();
        $this->assign('data', $row);
        $this->assign('userInfo', $userInfo);
        return $this->fetch();
    }
}
